package com.city.ajax.special;

import com.city.domain.city.CityTown;
import com.city.domain.report.CtEvent;
import com.city.repository.report.CtEventRepository;
import com.city.service.city.CityTownService;
import com.city.service.report.ReportService;
import com.city.service.report.impl.ReportServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
 * @Author 何平
 * @Description //TODO ShowAjax
 * @Date 13:41 2018/8/11
 * 评价数据返回
 **/
@Controller
@RequestMapping("ajax/ShowAjax")
public class ShowAjax {

    @Autowired
    private ReportServiceImpl reportService;
    @Autowired
    private CtEventRepository ctEventRepository;

    @Autowired
    private CityTownService cityTownService;

    /**
     * @Author 何平
     * @Description //TODO 区域评价数据信息
     * @Date 14:12 2018/8/11
     * @Param []
     * @return java.util.List<java.lang.Integer>
     **/
    @RequestMapping("/region_number")
    @ResponseBody
    public Map<String,Object> region_number(@RequestParam(value = "id") Integer id){
        Map<String,Object> map = new HashMap<>();
        List<Integer> years = reportService.findAllYear();
        List<CityTown> towns = cityTownService.findName("");
        Map<String,Object> yearNum = new HashMap<>();
        Map<String,Object> townNum = null;
        List<Integer> statusNum = null;
        map.put("report_default",reportService.findAllByScheduleAndTownId(0,id));
        map.put("report_accept",reportService.findAllByScheduleAndTownId(3,id));
        map.put("report_over",reportService.findAllByScheduleAndTownId(6,id));
        map.put("report_all",reportService.findAllByTownId(id));

        for (int year: years) {
            townNum = new HashMap<>();
            for (CityTown town:towns) {
                statusNum = new ArrayList<>();
                statusNum.add(reportService.findAllByYearAndScheduleAndTownId(year,0,town.getTownId()));
                statusNum.add(reportService.findAllByYearAndScheduleAndTownId(year,3,town.getTownId()));
                statusNum.add(reportService.findAllByYearAndScheduleAndTownId(year,6,town.getTownId()));
                townNum.put(town.getTownName(),statusNum);
            }
            yearNum.put(""+year,townNum);
        }
        map.put("years",years);
        map.put("towns",towns);
        map.put("all",yearNum);
        return map;
    }

    /**
     * @Author 何平
     * @Description //TODO 部门评价数据信息，派遣给某个部门才是上报的
     * @Date 15:42 2018/8/12
     * @Param []
     * @return java.util.Map<java.lang.String,java.lang.Object>
     **/
    @RequestMapping("/depart_number")
    @ResponseBody
    public Map<String,Object> depart_number(@RequestParam(value = "id") Integer id){
        Map<String,Object> map = new HashMap<>();
        List<Integer> years = reportService.findAllYear();
        Map<String,List<String>> months = new HashMap<>();



        map.put("report_send",reportService.findAllByBranchIdAndSchedule(id,2));
        map.put("report_accept",reportService.findAllByBranchIdAndSchedule(id,3));
        map.put("report_over",reportService.findAllByBranchIdAndSchedule(id,6));
        map.put("report_all",reportService.findAllByBranchId(id));

        map.put("years",years);
        for (int year:years) {
            months.put("send_"+year,reportService.findAllByYearAndBranchIdAndSchedule(year,id,2));
            months.put("over_"+year,reportService.findAllByYearAndBranchIdAndSchedule(year,id,6));
        }
        map.put("months",months);
        return map;
    }

    /**
     * @Author 何平
     * @Description //TODO 岗位评价，数字信息
     * @Date 11:11 2018/8/24
     * @Param []
     * @return java.util.Map<java.lang.String,java.lang.Object>
     **/
    @RequestMapping("/station_number")
    @ResponseBody
    public Map<String,Object> station_number(){
        Map<String,Object> map = new HashMap<>();
        String[] arrs = reportService.findAllByTypeBig();
        for (int i=0;i<arrs.length;i++) {
            String num = arrs[i].substring(0,arrs[i].indexOf(","));
          CtEvent ctEvent =  reportService.findEventById(Integer.parseInt(num));
          if(ctEvent == null){
              arrs[i].replaceAll(num,"");
          }else{
              arrs[i] = arrs[i].replaceFirst(num,ctEvent.getName());
          }
        }
        map.put("arrs",arrs);
        map.put("report_default",reportService.findAllByScheduleAndYear(0,2018));
        map.put("report_accept",reportService.findAllByScheduleAndYear(3,2018));
        map.put("report_over",reportService.findAllByScheduleAndYear(6,2018));
        map.put("report_all",reportService.findAllByYear(2018).size());

        map.put("send_months",reportService.findAllByYearAndSchedule(2018,0));
        map.put("over_months",reportService.findAllByYearAndSchedule(2018,6));
        return map;
    }
}
